/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>
#region Using Statements

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.ModuleToolkit.Common;

#endregion

namespace DotNetNuke.ModuleToolkit.Localization
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Provides an application-wide localization service
    /// </summary>
    /// <remarks>
    /// This Localization service is designed for usage in ASP.Net. All of the localization
    /// methods <see cref="Localization.LocalizeString"/> use the Control hierarchy to locate a control
    /// that implements the <see cref="ILocalizationService"/> interface. When calling
    /// <see cref="LocalizeString"/> you must always provide a "Reference Point" control.
    /// This is the control that will be used as the starting point of a recursive search up (NOT down)
    /// the control tree, using <see cref="Control.Parent"/>. When a control that implements
    /// <see cref="ILocalizationService"/> is found, this service will pass the parameters on
    /// to that service. This allows you to use file-based localization from within controls inside
    /// a private assembly.
    /// </remarks>
    /// <history>
    ///     [anurse]    05/04/2006  Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public static class Localization
    {

        #region Public Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the columns of the specified grid view using the header text of each
        /// column as the key, and using the grid as the reference point in the search for
        /// a Localization Service. Uses Local Resources
        /// </summary>
        /// <param name="grid">The grid to localize</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void LocalizeGridView(GridView grid)
        {
            LocalizeGridView(grid, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the columns of the specified grid view using the header text of each
        /// column as the key, and using the grid as the reference point in the search for
        /// a Localization Service. Uses Local Resources
        /// </summary>
        /// <param name="referencePoint">The reference point to use when searching for a Localization Service</param>
        /// <param name="grid">The grid to localize</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void LocalizeGridView(Control referencePoint, GridView grid)
        {
            LocalizeGridView(referencePoint, grid, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the columns of the specified grid view using the header text of each
        /// column as the key, and using the grid as the reference point in the search for
        /// a Localization Service. Uses Global or Local Resources depending on the value
        /// of <paramref name="useGlobalResources"/>.
        /// </summary>
        /// <param name="grid">The grid to localize</param>
        /// <param name="useGlobalResources">A boolean specifying if global or local resources are to be used</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void LocalizeGridView(GridView grid, bool useGlobalResources)
        {
            LocalizeGridView(grid, grid, useGlobalResources);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the columns of the specified grid view using the header text of each
        /// column as the key, and using the grid as the reference point in the search for
        /// a Localization Service. Uses Global or Local Resources depending on the value
        /// of <paramref name="useGlobalResources"/>.
        /// </summary>
        /// <param name="referencePoint">The reference point to use when searching for a Localization Service</param>
        /// <param name="grid">The grid to localize</param>
        /// <param name="useGlobalResources">A boolean specifying if global or local resources are to be used</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void LocalizeGridView(Control referencePoint, GridView grid, bool useGlobalResources)
        {
            foreach (DataControlField field in grid.Columns)
            {
                string currentHeader = field.HeaderText;
                if (!String.IsNullOrEmpty(currentHeader))
                    field.HeaderText = LocalizeString(referencePoint, currentHeader + ".HeaderText", currentHeader, useGlobalResources);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the specified string using the first LocalizationService located.
        ///  Uses Local Resources
        /// </summary>
        /// <param name="referencePoint">The control to start searching for ILocalizationServices from</param>
        /// <param name="key">The string to localize</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string LocalizeString(Control referencePoint, string key)
        {
            return LocalizeString(referencePoint, key, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the specified string using the local resource file for this module.
        /// Uses Local Resources
        /// </summary>
        /// <param name="referencePoint">The control to start searching for ILocalizationServices from</param>
        /// <param name="key">The string to localize</param>
        /// <param name="defaultValue">The value to return if the key can't be found</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string LocalizeString(Control referencePoint, string key, string defaultValue)
        {
            return LocalizeString(referencePoint, key, defaultValue, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the specified string using the first LocalizationService located.
        /// Uses Global or Local Resources depending on the value of <paramref name="useGlobalResources"/>.
        /// </summary>
        /// <param name="referencePoint">The control to start searching for ILocalizationServices from</param>
        /// <param name="key">The string to localize</param>
        /// <param name="useGlobalResources">A boolean specifying if global or local resources are to be used</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string LocalizeString(Control referencePoint, string key, bool useGlobalResources)
        {
            return LocalizeString(referencePoint, key, key, useGlobalResources);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Localizes the specified string using the local resource file for this module.
        /// Uses Global or Local Resources depending on the value of <paramref name="useGlobalResources"/>.
        /// </summary>
        /// <param name="referencePoint">The control to start searching for ILocalizationServices from</param>
        /// <param name="key">The string to localize</param>
        /// <param name="defaultValue">The value to return if the key can't be found</param>
        /// <param name="useGlobalResources">A boolean specifying if global or local resources are to be used</param>
        /// <history>
        ///     [anurse]  05/04/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string LocalizeString(Control referencePoint, string key, string defaultValue, bool useGlobalResources)
        {
            string resKey = key;
            if (resKey.IndexOf('.') == -1)
                resKey += ".Text";

            ILocalizationService localizationSvc = null;
            if (referencePoint is ILocalizationService)
                localizationSvc = (ILocalizationService)referencePoint;
            else
                localizationSvc = ControlUtils.FindParent<ILocalizationService>(referencePoint);

            if (localizationSvc != null)
                return localizationSvc.LocalizeString(resKey, defaultValue, useGlobalResources);
            else
                return defaultValue;
        }

        #endregion

    }
}
